package phapp.action.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.jquery.JqueryPageHelper;
import javacommon.jquery.ListRange;
import javacommon.util.StringTool;

import phapp.model.system.SysUser;
import phapp.model.system.TsUserRole;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;
import static javacommon.util.Struts2JsonHelper.*;

import javacommon.base.*;

import phapp.model.system.*;
import phapp.dao.system.*;
import phapp.service.system.impl.*;
import phapp.service.system.*;

/**
 * Title: ts_sys_user Description: 接收前面界面请求,通过json数据格式交互 Action Description:
 * 页面通过Ajax调用Action层,返回JSONO数据 Copyright: Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

public class SysUserAction extends BaseStruts2Action
{

   private SysUserManager sysuserManager;
   private TsUserRoleManager tsUserRoleManager;
   /** 通过spring自动注入 */
   public void setSysUserManager(SysUserManager manager)
   {
      this.sysuserManager = manager;
   }
   /** 通过spring自动注入 */
   public void setTsUserRoleManager(TsUserRoleManager manager)
   {
      this.tsUserRoleManager = manager;
   }
   /**
    * jqGrid使用列表 分页查询
    * 
    * @throws IOException
    * @author
    */
   public void list() throws IOException
   {
      PageRequest<Map> pr = JqueryPageHelper.createPageRequestForJquery(getRequest());
      Page page = sysuserManager.findByPageRequest(pr);

      List<SysUser> SysUserlist = (List) page.getResult();
      ListRange<SysUser> resultList = new ListRange<SysUser>();
      resultList.setDataRows(SysUserlist);
      resultList.setRecords(page.getTotalCount());
      resultList.setRows(page.getPageSize());
      if(page.getTotalCount() % page.getPageSize() > 0)
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize() + 1);
      }
      else
      {
         resultList.setTotal(page.getTotalCount() / page.getPageSize());
      }
      resultList.setPage(pr.getPageNumber());
      outJson(resultList);
   }

   /**
    * SysUser添加
    * 
    * @author
    * @throws IOException
    */
   public void save() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String sysuserJson = request.getParameter("sysuserJson");
         if(null != sysuserJson && !"".equals(sysuserJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(sysuserJson);
            SysUser sysuserobj = (SysUser) JSONObject.toBean(jsonobject, SysUser.class);
            sysuserManager.save(sysuserobj);
            result.put("success", true);
            result.put("msg", "添加成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "添加失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "添加失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * SysUser修改
    * 
    * @throws IOException
    * @author
    */
   public void update() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String sysuserJson = request.getParameter("sysuserJson");
         if(null != sysuserJson && !"".equals(sysuserJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(sysuserJson);
            SysUser sysuserobj = (SysUser) JSONObject.toBean(jsonobject, SysUser.class);

            /** 设置主键值 */
            // sysuserobj.setId(jsonobject.get("id").toString());
            sysuserobj.setId(Integer.parseInt(jsonobject.get("userId").toString()));

            sysuserManager.update(sysuserobj);
            result.put("success", true);
            result.put("msg", "修改成功!");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }

   /**
    * SysUser删除
    * 
    * @throws IOException
    * @author
    */
   public void delete() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try {
         HttpServletRequest request = getRequest();
         String ids = request.getParameter("ids");
         if(null != ids && !"".equals(ids))
         {
            SysUser sysuser = new SysUser();
            sysuser.setIdList(StringTool.stringToArray(ids));
            sysuserManager.removeByIdArry(sysuser);
            result.put("success", true);
            result.put("msg", "删除成功");
         }
         else
         {
            result.put("success", false);
            result.put("msg", "删除失败 数据错误!");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "删除失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
   
   
   public void updatePassword() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String sysuserJson = request.getParameter("sysuserJson");
         if(null != sysuserJson && !"".equals(sysuserJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(sysuserJson);
            SysUser sysuserobj = (SysUser) JSONObject.toBean(jsonobject, SysUser.class);

            sysuserobj.setId(Integer.parseInt(jsonobject.get("id").toString()));

            result = sysuserManager.updatePassword(sysuserobj);
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败，数据错误！");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "系统异常，修改失败！");
         e.printStackTrace();
      }
      
      outJson(result);
   }
   
   public void resetSysUserPassword() throws IOException
   {
      Map<String, Object> result = new HashMap<String, Object>();
      try
      {
         HttpServletRequest request = getRequest();
         String sysuserJson = request.getParameter("sysuserJson");
         if(null != sysuserJson && !"".equals(sysuserJson))
         {
            JSONObject jsonobject = JSONObject.fromObject(sysuserJson);
            SysUser sysuserobj = (SysUser) JSONObject.toBean(jsonobject, SysUser.class);

            sysuserobj.setId(Integer.parseInt(jsonobject.get("id").toString()));

            result = sysuserManager.resetSysUserPassword(sysuserobj);
         }
         else
         {
            result.put("success", false);
            result.put("msg", "修改失败，数据错误！");
         }
      }
      catch(Exception e)
      {
         result.put("success", false);
         result.put("msg", "系统异常，修改失败！");
         e.printStackTrace();
      }
      
      outJson(result);
   }


   /**
    * 修改角色
    * @throws IOException
    * @author
    */
   public void fuquan() throws IOException {
      Map<String, Object> result = new HashMap<String, Object>();
      try  {
         HttpServletRequest request = getRequest();
         String roleuserJson = request.getParameter("roleuserJson");
         if(null != roleuserJson && !"".equals(roleuserJson))  {
            JSONObject jsonobject = JSONObject.fromObject(roleuserJson);
            SysUser sysuserobj = (SysUser) JSONObject.toBean(jsonobject, SysUser.class);
            String userId = jsonobject.get("userId").toString();
            String roles = jsonobject.get("roles").toString();
            /** 设置主键值 */ 
            sysuserobj.setId(Integer.parseInt(jsonobject.get("userId").toString()));
            Map<String, Object> resultxx = new HashMap<String, Object>();
            resultxx.put("userId", userId);
            tsUserRoleManager.deleteByObj(resultxx);
            String[] strs = StringTool.SplitString(roles,",");
            for(int i=0;null!=strs&&i<strs.length;i++){
            	String roleid = strs[i].trim().toString();
            	if(StringTool.isNumeric(roleid)){
            		TsUserRole u = new TsUserRole();
	            	u.setRoleId(Integer.parseInt(strs[i].trim().toString()));
	            	u.setUserId(Integer.parseInt(userId));
	            	u.setDataStatus(1);
	            	tsUserRoleManager.save(u);
            	}
            }
            result.put("success", true);
            result.put("msg", "修改成功!");
         }  else  {
            result.put("success", false);
            result.put("msg", "修改失败 数据错误!");
         }
      }  catch(Exception e)  {
         result.put("success", false);
         result.put("msg", "修改失败!");
         e.printStackTrace();
      }
      outJson(result);
   }
   
   
   
   
   
   
}
